<?php
/**
 * @description 库存管理类
 * @access public
 * @author xiayuanjun@shopex.cn
 * @ver 0.1
 */
class ome_store_manage{

    /**
     *
     * 静态私有变量仓库类型
     * @var array
     */
    static private $__branchType = '';

    /**
     *
     * 静态私有变量仓库类型白名单定义
     * @var array
     */
    static private $__branchTypes = array(
        'branch'=>array('type' => 'branch','desc' => "电商仓"),
        'store'=>array('type' => 'store','desc' => "门店"),
    );

    /**
     *
     * 静态私有变量处理模式对象
     * @var array
     */
    static private $__processObj = null;

    /**
     *
     * 静态私有变量事件节点
     * @var array
     */
    static private $__node = '';

    /**
     *
     * 静态私有变量事件节点列表
     * @var array
     */
    static private $__nodeTypes = array(
        'addDly'=>array('method' => 'addDly','desc' => "新建发货单"),
        'cancelDly'=>array('method' => 'cancelDly','desc' => "取消发货单"),
        'consignDly'=>array('method' => 'consignDly','desc' => "发货"),
        'pauseOrd'=>array('method' => 'pauseOrd','desc' => "暂停订单"),
        'renewOrd'=>array('method' => 'renewOrd','desc' => "恢复订单"),
    );

    /**
     *
     * 静态私有变量是否管控库存
     * @var array
     */
    static private $__isCtrlStore = '';

    /**
     *
     * 静态私有变量应用级参数
     * @var array
     */
    static private $_appParams = array();

    public function isCtrlBranchStore(){
        return self::$__isCtrlStore;
    }

    public function isStoreBranch(){
        return (self::$__branchType == 'store') ? true : false;
    }

    public function processBranchStore($params, &$err_msg){

        //必要参数检查
        if(!$this->checkParams($params)){
            return false;
        }

        //识别处理事件节点
        if(!$this->identifyNode($params)){
            return false;
        }

        //执行模式下的库存处理
        $method    = self::$__node;
        return self::$__processObj->$method(self::$_appParams, $err_msg);
    }

    /**
     * 加载库存的处理模式
     *
     * @param array $params
     * @return boolean true/false
     */
    public function loadBranch($params){

        //识别仓库类型
        if(!$this->identifyBranchType($params)){
            return false;
        }

        //识别库存管控
        $this->identifyCtrlStore($params);

        $class_name = sprintf('ome_store_manage_%s', self::$__branchType);
        try{
            if (class_exists($class_name)){
                self::$__processObj = kernel::single($class_name, self::$__isCtrlStore);
                return self::$__processObj;
            }else{
                return false;
            }
        } catch (Exception $e) {
            return false;
        }
    }

    /**
     * 识别仓库类型
     *
     * @param array $params
     * @return boolean true/false
     */
    private function identifyBranchType($params){

        if(isset($params['branch_type']) && isset(self::$__branchTypes[$params['branch_type']])){
            self::$__branchType = self::$__branchTypes[$params['branch_type']]['type'];
        }elseif(isset($params['branch_id'])){
            $branchObj = app::get('ome')->model('branch');
            $branchInfo = $branchObj->db->selectrow("SELECT branch_id,b_type FROM sdb_ome_branch WHERE branch_id='".$params['branch_id']."'");
            if($branchInfo){
                if($branchInfo['b_type'] == 1){
                    //电商仓
                    self::$__branchType = 'branch';
                }elseif($branchInfo['b_type'] == 2){
                    //门店线下仓
                    self::$__branchType = 'store';
                }
            }
        }

        //识别不了当前处理仓库类型
        if(!self::$__branchType){
            return false;
        }else{
            return true;
        }
    }

    /**
     * 识别仓库是否管控库存
     *
     * @param array $params
     * @return boolean true/false
     */
    private function identifyCtrlStore($params){

        self::$__isCtrlStore = true;

        //判断当前模式是否管控库存，电商仓默认就是管控库存
        if(self::$__branchType == 'store'){
            $storeObj = app::get('o2o')->model('store');
            $storeInfo = $storeObj->db->selectrow("SELECT is_ctrl_store FROM sdb_o2o_store WHERE branch_id='".$params['branch_id']."'");
            if($storeInfo){
                //如果门店指定不管控库存
                if($storeInfo['is_ctrl_store'] == 2){
                    self::$__isCtrlStore = false;
                }
            }
        }
    }

    /**
     * 识别库存处理的事件节点
     *
     * @param array $params
     * @return boolean true/false
     */
    private function identifyNode($params){

        if(isset($params['node_type']) && isset(self::$__nodeTypes[$params['node_type']])){
            self::$__node = self::$__nodeTypes[$params['node_type']]['method'];
            return true;
        }else{
            return false;
        }
    }

    /**
     * 检查传入参数
     *
     * @param array $params
     * @return boolean true/false
     */
    private function checkParams($params){

        self::$_appParams = $params['params'];

        return true;
    }

}